<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

    <link rel="stylesheet" th:href="@{/layui/css/layui.css}" >
    <script th:src="@{/layui/layui.js}" charset="utf-8"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<br>
<form class="layui-form" action="" id="searchform">
    <div class="layui-form-item">
        <label class="layui-form-label">WeChat号</label>
        <div class="layui-input-inline">
            <input type="text" name="wechat_no" id="wechat_no" placeholder="请输入WeChat号" autocomplete="off" class="layui-input">
        </div>
        <label class="layui-form-label">WeChat备注</label>
        <div class="layui-input-inline">
            <input type="text" name="wechat_mark"  id='wechat_mark' placeholder="请输入WeChat备注" autocomplete="off" class="layui-input">
        </div>
        <label class="layui-form-label">QQ</label>
        <div class="layui-input-inline">
            <input type="text" name="qq_no" id='qq_no' placeholder="请输入qq" autocomplete="off" class="layui-input">
        </div>

        <div class="site-demo-button" id="top_options" style="margin-bottom: 0;margin-right: 20px" align="right" >
            <button class="layui-btn layui-btn-sm" type="button" onclick="doSearch();">Search</button>
            <button type="reset" class="layui-btn layui-btn-sm" >Rest</button>
        </div>
    </div>
</form>
<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
    </div>
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script>
    var $;
    layui.use('table', function(){
        var table = layui.table;
        var layer = layui.layer;
        $ = layui.jquery;
        table.render({
            elem: '#test'
            ,url:'/courseorder/listJson'
            ,toolbar: '#toolbarDemo'
            ,title: '用户数据表'
            ,totalRow: true
            ,cols: [
                [
                {type: 'checkbox', fixed: 'left'}
                ,{field:'name', title:'名字', }
                ,{field:'wechat_mark', title:'微信备注'}
                ,{field:'wechat_no', title:'微信ID', sort: true}
                ,{field:'qq_no', title:'QQ号', }
                ,{field:'course_name', title:'购买课程'}
                ,{field:'course_price', title:'价格',  sort: true, totalRow: true}
                ,{field:'order_date', title:'订单日期',}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo',}
                ]
            ]
            ,page: true
            ,limit:7
            ,limits:[7,8,9]
        });

        //工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选')
                    break;
                case 'add':
                    openAddPage();
                    break;
            };
        });

        //监听行双击事件
        table.on('rowDouble(test)', function(obj){
            //obj 同上
            var order_id = obj.data.order_id;
            parent.layer.open({
                type:2,//2跳转到页面
                offset: 'auto',
                title:'课程订单详情',
                content:'/courseorder/detail?order_id='+order_id,
                area: ['1000px', '600px'],
                btn:'关闭全部',
                btnAlign:'c',
                yes:function () {
                    parent.layer.closeAll();
                }
            });
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                ,layEvent = obj.event; //获得 lay-event 对应的值
            if(layEvent === 'edit'){
                openEditPage(obj);
            } else if(layEvent === 'del'){
                layer.confirm('真的删除行么' + obj.data.order_id, function(index){
                //obj.del(); //删除对应行（tr）的DOM结构
                    console.log(index);
                    layer.close(index);
                        //向服务端发送删除指令
                    doDelete(obj);
                });
            }
        });

        function doDelete(obj) {
            var oId = obj.data.order_id;
            $.get('/courseorder/delete',{order_id:oId},function (resp) {
                //console.log(resp);
                if(resp.success == 1){
                    layer.msg("删除成功");
                    obj.del(); //删除对应行（tr）的DOM结构
                }else{
                    layer.msg("删除失败" + resp.message);
                }
            });
        }
        function openAddPage() {
            parent.layer.open({
                type:2,//2跳转到页面
                offset: 'auto',
                title:'添加课程订单',
                content:'/courseorder/add',
                area: ['1000px', '600px'],
                btn:'关闭全部',
                btnAlign:'c',
                yes:function () {
                    parent.layer.closeAll();
                }
            });
        }

        function openEditPage(obj) {
            var order_id = obj.data.order_id;
            parent.layer.open({
                type:2,//2跳转到页面
                offset: 'auto',
                title:'修改课程订单',
                content:'/courseorder/edit?order_id='+order_id,
                area: ['1000px', '600px'],
                btn:'关闭全部',
                btnAlign:'c',
                yes:function () {
                    parent.layer.closeAll();
                }
            });
        }
    });
    
    function doDelete() {
        
    }
</script>

</body>
</html>